package com.water.bs.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.water.bs.pojo.Gift;
import com.water.bs.pojo.GiftHistory;
import com.water.bs.pojo.User;
import com.water.bs.service.GiftManageService;
import com.water.bs.service.UserGiftService;
import com.water.bs.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@CrossOrigin
@RestController
public class UserGiftController {
    @Autowired
    private UserGiftService userGiftService;
    @Autowired
    private UserService userService;
    @Autowired
    private GiftManageService giftManageService;
    @RequestMapping("/queryAbleGift")
    public JSON queryableGift(){
        JSONObject rs = new JSONObject();
        List<Gift> gifts = null;
        gifts = userGiftService.queryAbleGift();
        rs.put("data",gifts);
        return rs;
    }

    @RequestMapping("/exchangeGift") //用户兑换礼物
    public JSON exchangeGift(@RequestBody JSONObject exchangeGiftInfo){
        JSONObject rs = new JSONObject();
        System.out.println(exchangeGiftInfo);
        User user = userService.getUserById((String) exchangeGiftInfo.get("userid"));
        Gift gift = userGiftService.qryGiftById((String) exchangeGiftInfo.get("giftId"));
        if (user.getShoppoint()<gift.getPrice()){
            rs.put("state","1");
            rs.put("message","可用积分不足!");
            return rs;
        }
        Map<String,Object>userUpdateMap = new HashMap<>();
        userUpdateMap.put("shoppoint",user.getShoppoint()-gift.getPrice());//兑换礼物扣积分
        userUpdateMap.put("id",exchangeGiftInfo.get("userid"));
        int i = userService.updateUser(userUpdateMap);
        if (i<1){
            rs.put("state","2");
            rs.put("message","兑换失败!");
            return rs;
        }
        Map<String,Object>giftUpdateMap = new HashMap<>();
        giftUpdateMap.put("stock",gift.getStock()-1);//库存-1
        giftUpdateMap.put("id",gift.getId());
        int i1 = giftManageService.updateGift(giftUpdateMap);
        if (i1<1){
            rs.put("state","2");
            rs.put("message","兑换失败!");
            return rs;
        }
        Map<String,Object> gifthistoryMap = new HashMap<>();
        gifthistoryMap.put("id", UUID.randomUUID().toString());
        gifthistoryMap.put("userid", exchangeGiftInfo.get("userid"));
        gifthistoryMap.put("giftid", exchangeGiftInfo.get("giftId"));
        gifthistoryMap.put("state", 0);//初次状态0  未处理
        gifthistoryMap.put("createtime", System.currentTimeMillis());//时间毫秒
        int i2 = userGiftService.exchangeGift(gifthistoryMap);//兑换礼物信息插入到gifthistory表
        if (i2>0) {
            rs.put("state","0");
            rs.put("message","兑换成功!");
            return  rs;
        }else {
            rs.put("state", "2");
            rs.put("message", "兑换失败!");
            return rs;
        }
    }

    @RequestMapping("/qryGiftHistory") //查询礼物兑换历史
    public JSON qryGiftHistory(@RequestParam("id")String id){
        System.out.println(id);
        JSONObject rs = new JSONObject();
        List<Map<String, Object>> maps = userGiftService.qryGiftHistory(id);
        System.out.println(maps);
        rs.put("data",maps);
        return rs;
    }

    @RequestMapping("/cancelGiftBuy")
    public JSON cancelGiftBuy(@RequestBody JSONObject cancelInfo){
        JSONObject rs = new JSONObject();
        System.out.println("需要取消的兑换订单id:"+cancelInfo.get("id"));
        System.out.println("取消的礼物ID:"+cancelInfo.get("giftId"));
        System.out.println("本次操作的用户:"+cancelInfo.get("userid"));
        int i = userGiftService.cancelGiftBuy((String) cancelInfo.get("id"));
        if (i>0){
            User user = userService.getUserById((String) cancelInfo.get("userid"));
            Gift gift = userGiftService.qryGiftById((String) cancelInfo.get("giftId"));
            Map<String,Object> map = new HashMap<>();
            map.put("shoppoint",user.getShoppoint()+gift.getPrice());
            map.put("id",cancelInfo.get("userid"));
            int i1 = userService.updateUser(map);//取消后 获取礼物的价格 shoppoint加回去 然后把礼物的库存加回去
            Map<String,Object>giftUpdateMap = new HashMap<>();
            giftUpdateMap.put("stock",gift.getStock()+1);//用户取消兑换。库存+1改回去
            giftUpdateMap.put("id",gift.getId());
            int i11 = giftManageService.updateGift(giftUpdateMap);
            if (i1>0 && i11>0){
                rs.put("state","0");
                rs.put("message","操作成功!");
                return rs;
            }else{
                rs.put("state","2");
                rs.put("message","操作失败!");
                return rs;
            }

        }else{
            rs.put("state","1");
            rs.put("message","修改订单状态失败!");
            return rs;
        }
    }


}
